Home:ALL Converter>Regular expression logic

Regular expression logic

Ask Time:2018-11-20T23:29:12         Author:claudioz

Json Formatter

I'm developing a web application using Angular 6. I have a question: I'm creating a custom input component (for text input) such as:

@Component({
  selector: 'input-text',
  templateUrl: './input-text.component.html'
  ]
})
export class InputTextComponent {

  @Input() pattern?: string;

}

I would like a user can insert a regular expression for the validation of the input field, in this way:

<input-text pattern="^[a-z0-9_-]{8,15}$"></input-text>

The template of my component is defined like this:

<input type="text" [attr.pattern]="pattern"/>

Unfortunately I know absolutely nothing about regular expressions. I would like to do two things:

1 - Create a method that checks the validity of the regular expression and changes the visual style.

2 - Make sure that if the input (with a pattern field) is inserted into a form, the attribute form.valid remains false until the expression is valid. Thanks for your help!

Author:claudioz,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53396321/regular-expression-logic
yy